﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acme.TestSite.Objects;
using Acme.Northwind.EFDAL;
using Acme.Northwind.EFDAL.Entity;

namespace Acme.TestSite
{
	public partial class OrderItem : BasePersistablePage
	{
		#region Page Events

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			cmdSave.Click += new EventHandler(SaveData);
			cmdCancel.Click += new EventHandler(cmdCancel_Click);
		}

		#endregion

		#region Methods

		protected override nHydrate.EFCore.DataAccess.NHEntityObject CreateObject(NorthwindEntities context)
		{
			var newItem = new Acme.Northwind.EFDAL.Entity.Order();
			context.AddItem(newItem);
			return newItem;
		}

		protected override void SetupBindings(NorthwindEntities context)
		{
			var orderId = this.Request["id"].ToInt32();
			var order = context.Order.FirstOrDefault(x => x.OrderId == orderId);
			if (order == null)
				order = CreateObject(context) as Acme.Northwind.EFDAL.Entity.Order;

			Mapper.Map(lblOrderId, order, Acme.Northwind.EFDAL.Entity.Order.FieldNameConstants.OrderId);
			Mapper.Map(txtAddress, order, Acme.Northwind.EFDAL.Entity.Order.FieldNameConstants.ShipAddress);
			Mapper.Map(txtCity, order, Acme.Northwind.EFDAL.Entity.Order.FieldNameConstants.ShipCity);
			Mapper.Map(txtZip, order, Acme.Northwind.EFDAL.Entity.Order.FieldNameConstants.ShipPostalCode);
		}

		#endregion

		#region Event Handlers

		private void cmdCancel_Click(object sender, EventArgs e)
		{
			this.Response.Redirect(SessionHelper.LastOrderListSearch);
		}

		protected override void OnDataSavedSucess(EventArgs e)
		{
			base.OnDataSavedSucess(e);
			this.Response.Redirect(SessionHelper.LastOrderListSearch);
		}

		#endregion

	}
}